package com.hunau.web.controller.terminal;

import java.util.ArrayList;
import java.util.List;

import com.hunau.common.core.domain.Ztree;
import com.hunau.framework.util.ShiroUtils;
import com.hunau.system.domain.SysDept;
import com.hunau.system.service.ISysDeptService;
import com.hunau.system.service.ITerminalsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hunau.common.annotation.Log;
import com.hunau.common.enums.BusinessType;
import com.hunau.system.domain.TbEnergy;
import com.hunau.system.service.ITbEnergyService;
import com.hunau.common.core.controller.BaseController;
import com.hunau.common.core.domain.AjaxResult;
import com.hunau.common.utils.poi.ExcelUtil;
import com.hunau.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 能源数据采集Controller
 * 
 * @author ruoyi
 * @date 2020-03-17
 */
@Controller
@RequestMapping("/system/energy")
public class TbEnergyController extends BaseController
{
    private String prefix = "terminal/energy";
    @Autowired
    private ITerminalsService terminalsService;
    @Autowired
    private ITbEnergyService tbEnergyService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("system:energy:view")
    @GetMapping()
    public String energy()
    {
        return prefix + "/energy";
    }

    /**
     * 查询能源数据采集列表
     */
    @RequiresPermissions("system:energy:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbEnergy tbEnergy)
    {
        startPage();
        List<TbEnergy> list = tbEnergyService.selectTbEnergyList(tbEnergy);
        return getDataTable(list);
    }

    /**
     * 导出能源数据采集列表
     */
    @RequiresPermissions("system:energy:export")
    @Log(title = "能源数据采集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbEnergy tbEnergy, @RequestParam(value = "ids",required = false) ArrayList<Object> ids )
    {
        System.out.println(ids);
        List<TbEnergy> list = tbEnergyService.selectTbEnergyListByIds(ids);
        ExcelUtil<TbEnergy> util = new ExcelUtil<TbEnergy>(TbEnergy.class);
        return util.exportExcel(list, "energy");
    }

    /**
     * 新增能源数据采集
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存能源数据采集
     */
    @RequiresPermissions("system:energy:add")
    @Log(title = "能源数据采集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbEnergy tbEnergy)
    {
        return toAjax(tbEnergyService.insertTbEnergy(tbEnergy));
    }

    /**
     * 修改能源数据采集
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbEnergy tbEnergy = tbEnergyService.selectTbEnergyById(id);
        mmap.put("tbEnergy", tbEnergy);
        return prefix + "/edit";
    }
    @Log(title = "环境数据采集", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:energy:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TbEnergy> util = new ExcelUtil<TbEnergy>(TbEnergy.class);
        List<TbEnergy> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = tbEnergyService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("system:energy:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TbEnergy> util = new ExcelUtil<TbEnergy>(TbEnergy.class);
        return util.importTemplateExcel("能源数据");
    }
    /**
     * 修改保存能源数据采集
     */
    @RequiresPermissions("system:energy:edit")
    @Log(title = "能源数据采集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbEnergy tbEnergy)
    {
        return toAjax(tbEnergyService.updateTbEnergy(tbEnergy));
    }

    /**
     * 删除能源数据采集
     */
    @RequiresPermissions("system:energy:remove")
    @Log(title = "能源数据采集", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbEnergyService.deleteTbEnergyByIds(ids));
    }
//    /**
//     * 加载部门列表树
//     */
//    @RequiresPermissions("system:terminals:list")
//    @GetMapping("/treeData")
//    @ResponseBody
//    public List<Ztree> treeData()
//    {
//        List<Ztree> ztrees = terminalsService.selectAddrTree(new AddrTree());
//        return ztrees;
//    }
    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = deptService.selectDeptTree(new SysDept());
        return ztrees;
    }
}
